package cc.rengu.redp.flowable.listener;

import cc.rengu.redp.common.utils.SpringContextUtil;
import cc.rengu.redp.flowable.domain.CustomUserNode;
import cc.rengu.redp.flowable.service.SysWorkFlowService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Service;

import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.*;

@Slf4j
@Service
public class GlobalEventListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        log.debug("GlobalListener:{}", flowableEvent.getType().name());

        FlowableEventType type = flowableEvent.getType();
        if (TASK_CREATED.equals(type)) {
            eventHandlerTaskCreated(getTaskEntity(flowableEvent));
        } else if (TASK_COMPLETED.equals(type)) {
            eventHandlerTaskCompleted(getTaskEntity(flowableEvent));
        } else if (PROCESS_STARTED.equals(type)) {
            eventHandlerProcessStarted(getExecutionEntity(flowableEvent));
        } else if (PROCESS_COMPLETED.equals(type)) {
            eventHandlerProcessCompleted(getExecutionEntity(flowableEvent));
        } else {
            return;
        }
    }

    private void eventHandlerProcessCompleted(ExecutionEntity executionEntity) {
    }

    private void eventHandlerProcessStarted(ExecutionEntity executionEntity) {
    }

    private void eventHandlerTaskCompleted(TaskEntity taskEntity) {
    }

    private void eventHandlerTaskCreated(TaskEntity taskEntity) {
        SysWorkFlowService sysWorkFlowService = SpringContextUtil.getBean(SysWorkFlowService.class);

        CustomUserNode customUserNode = sysWorkFlowService.getCustomNodeByTaskId(taskEntity.getId());
        if (customUserNode.isSetAssignToApplicant()){
            taskEntity.setAssignee(sysWorkFlowService.getApplicantByProcInstId(taskEntity.getProcessInstanceId()));
        } else if (customUserNode.isSetHistUserFirst()) {
            String assignee = sysWorkFlowService.getHistAssigneeInThisProcInstByTaskId(taskEntity.getId());
            if (assignee != null) {
                taskEntity.setAssignee(assignee);
            }
        }
    }

    /**
     * 转换事件内容为TaskEntity
     * @param event
     * @return
     */
    private TaskEntity getTaskEntity(FlowableEvent event){
        FlowableEntityEvent entityEvent = (FlowableEntityEvent) event;
        return (TaskEntity) entityEvent.getEntity();
    }

    /**
     * 转换事件内容为ExcutionEntity
     * @param event
     * @return
     */
    private ExecutionEntity getExecutionEntity(FlowableEvent event){
        FlowableEntityEvent entityEvent = (FlowableEntityEvent) event;
        return (ExecutionEntity) entityEvent.getEntity();
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
